package ml.uuku.swipeheadertabs;

import ml.uuku.swipeheadertabs.ui.fragment.BooksListFragment;

import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import butterknife.Bind;
import butterknife.ButterKnife;

import com.bumptech.glide.Glide;

public class MainActivity extends AppCompatActivity implements AppBarLayout.OnOffsetChangedListener {

    @Bind(R.id.appbar)
    AppBarLayout appBarLayout;
    @Bind(R.id.collapsing_toolbar)
    CollapsingToolbarLayout collapsingToolbarLayout;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.iv_header)
    ImageView headerView;
    @Bind(R.id.fab)
    FloatingActionButton floatingActionButton;

    private BooksListFragment fragment;
    private int verticalOffset = 0;// AppBarLayout

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        setSupportActionBar(toolbar);
        collapsingToolbarLayout.setTitle("图灵教育");
        appBarLayout.addOnOffsetChangedListener(this);
        Glide.with(this).load("http://c.hiphotos.baidu.com/image/pic/item/241f95cad1c8a786bbb339166509c93d70cf5008.jpg")
                .placeholder(R.drawable.place_holder)
                .centerCrop().into(headerView);
        floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 这里只是简单示意
                Toast.makeText(MainActivity.this, "Hi,I am floating action button", Toast.LENGTH_SHORT).show();
            }
        });

        setContentView(savedInstanceState);

    }

    private void setContentView(Bundle savedInstanceState) {
        if (savedInstanceState == null) {
            if (findViewById(R.id.content_container) != null) {
                fragment = new BooksListFragment();
                getSupportFragmentManager().beginTransaction()
                        .add(R.id.content_container, fragment,
                                "books")
                        .commit();
            }
        }
    }

    /**
     * 参考代码http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0616/3052.
     * html解决SwipeRefreshLayout与CollapsingToolbarLayout共用时的事件冲突
     * 
     * @param appBarLayout
     * @param i
     */
    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int i) {

        verticalOffset = i;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                if (verticalOffset == 0) {
                    fragment.setSwipeRefreshEnable(true);
                } else {
                    fragment.setSwipeRefreshEnable(false);
                }
                break;

        }
        return super.dispatchTouchEvent(ev);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        appBarLayout.removeOnOffsetChangedListener(this);
    }
}
